package com.zx.article.reviewRecords.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zx.article.reviewRecords.dao.IReviewRecordsDao;
import com.zx.article.reviewRecords.model.ReviewRecordsModel;
import com.zx.article.util.DBUtils;

public class ReviewRecordsDaoImpl implements IReviewRecordsDao {

	private DBUtils dbUtils = new DBUtils();

	@Override
	public int getCount(ReviewRecordsModel recordsModel) {
		String sqlString = "SELECT COUNT(1) FROM review_records WHERE r_article_id = ? ";
		Object[] valObjects = { recordsModel.getId() };
		return dbUtils.queryCount(sqlString, valObjects);
	}

	@Override
	public List<ReviewRecordsModel> queryList(ReviewRecordsModel recordsModel) {
		String sqlString = "SELECT *,(SELECT e_nick_name FROM editor WHERE editor.e_account = review_records.r_editor_id) AS editor FROM review_records WHERE r_article_id = ? ";
		Object[] valObjects = { recordsModel.getId() };
		Map<String, String> map = getMap();
		map.put("editorName", "editor");
		return dbUtils.queryList(sqlString, valObjects, ReviewRecordsModel.class, map);
	}

	private Map<String, String> getMap() {
		Map<String, String> map = new HashMap<>();
		map.put("id", "r_id");
		map.put("editorId", "r_editor_id");
		map.put("article", "r_article_id");
		map.put("result", "r_result");
		map.put("message", "r_message");
		map.put("date", "r_date");
		return map;
	}

	@Override
	public String insert(ReviewRecordsModel recordsModel) {
		String sqlString = "INSERT INTO review_records(r_editor_id,r_article_id,r_result,r_message,r_date) VALUES(?,?,?,?,?)";
		Object[] valObjects = { recordsModel.getEditorId(), recordsModel.getArticle(), recordsModel.getResult(),
				recordsModel.getMessage(), recordsModel.getDate() };
		return dbUtils.update(sqlString, valObjects);
	}

	@Override
	public String updPass(Integer articleInteger) {
		String sqlString = "UPDATE review_records SET r_result = 'pass' WHERE r_article_id = ?";
		Object[] valObjects = { articleInteger };
		return dbUtils.update(sqlString, valObjects);
	}

	@Override
	public String delete(Integer articleInteger) {
		String sqlString = "DELETE FROM review_records WHERE r_article_id = ? ";
		Object[] valObjects = { articleInteger };
		return dbUtils.update(sqlString, valObjects);
	}

}
